package app

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
	"testPaper/common"
	"testPaper/controller/web"
	"testPaper/model"
	"testPaper/response"
)

// QueryCase 小程序获取案例题目
func QueryCase(c *gin.Context) {
	DB := common.GetDB()
	paperId := c.PostForm("paperId")
	pageSize, _ := strconv.Atoi(c.PostForm("pageSize"))
	pageIndex, _ := strconv.Atoi(c.PostForm("pageIndex"))

	switch {
	case pageSize >= 100:
		pageSize = 100
	case pageSize <= 0:
		pageSize = 10
	}

	if pageIndex <= 0 {
		pageIndex = 1
	}

	if web.CaseExits(DB, paperId) {
		caseList := make([]model.Case, 0)
		var total int

		DB.Where("paper_id = ?", paperId).Limit(pageSize).Offset((pageIndex - 1) * pageSize).Find(&caseList).Offset(-1).Limit(-1).Count(&total)
		response.Reponse(c, http.StatusOK, 200, gin.H{
			"data":     caseList,
			"total":    total,
			"page":     pageIndex,
			"pageSize": pageSize,
		}, "获取成功")
		return

	} else {
		response.Reponse(c, http.StatusOK, 422, nil, "传入的试卷Id不存在")
		return
	}
}
